package org.neuedu.filter;

import org.neuedu.bean.User;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 分析出哪些请求需要登录才能访问
 */
@WebFilter(filterName = "Filter2_ParseUrlFilter",urlPatterns = {"/add","/article/reply"})
public class Filter2_ParseUrlFilter implements Filter {
    public void destroy() {
    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        HttpServletRequest req1 = (HttpServletRequest) req;
        User fly_user = (User) req1.getSession().getAttribute("fly_user");
        if (fly_user != null) {
            chain.doFilter(req, resp);
        }
        else{
            HttpServletResponse resp1 = (HttpServletResponse) resp;
            resp1.sendRedirect(req1.getContextPath()+"/login");
        }
    }

    public void init(FilterConfig config) throws ServletException {

    }

}
